//
//  SJWelcomeViewController.swift
//  SJWeiboSwift
//
//  Created by app;e on 15/10/4.
//  Copyright © 2015年 world. All rights reserved.
//
//  欢迎界面

import UIKit
import SDWebImage

class SJWelcomeViewController: UIViewController {
    /// 头像的底部约束
    var bottomConstraint: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if let urlString = sharedUserAccount?.avatar_large {
            iconView.sd_setImageWithURL(NSURL(string: urlString))
        }
        
        //头像动画
        bottomConstraint?.constant = -(UIScreen.mainScreen().bounds.height - 200)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
                self.view.layoutIfNeeded()
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //欢迎界面显示完毕,发送通知给appdelegate切换根控制器
                        //object:为true 表示切换到mainTabBarVC
                        NSNotificationCenter.defaultCenter().postNotificationName(SJSwitchRootVCNotification, object: true)
                })
        }
    }

    ///布局子控件
    override func loadView() {
        view = UIView()
        //背景
        view.addSubview(bgView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        //bgView添加约束
//        bgView.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subView]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":bgView]))
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subView]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":bgView]))
        
        bgView.ff_Fill(view)
       let cons = iconView.ff_AlignInner(ff_AlignType.BottomCenter, referView: view, size: nil, offset: CGPoint(x: 0, y: -160))
        bottomConstraint = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        //iconView添加约束
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//       bottomConstraint = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
//        view.addConstraint(bottomConstraint!)
        
    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subView(85)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":iconView]))
    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(85)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subView":iconView]))
        
        ///label添加约束
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 6))
    }
    
    ///背景
    lazy var bgView: UIImageView = {
    
        let bgImage = UIImageView(image: UIImage(named: "ad_background"))
        return bgImage
    }()
    ///头像
    lazy var iconView: UIImageView = {
        
        let iconImage = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconImage.layer.masksToBounds = true
        iconImage.layer.cornerRadius = 42.5
        return iconImage
    }()
    
    ///欢迎label
    lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.alpha = 0
        label.text = "欢迎回来"
        label.sizeToFit()
        return label
    }()
}
